<?php

namespace app\model;

use think\facade\Db;
use think\Model;

class ActiveParticipantsModel extends Model {

    protected $table ="active_participants";

    protected $pk = 'id';

    private $cacheKey="active:id:";

    private $cacheTime=5184000;


    /*
       * 获取total
       * */
    public function getTotal($where=[]){
        return Db::name($this->table)->where($where)->count();
    }


    /*
     * 获取list
     * */
    public function getList($where=[],$page = null,$limit = null,$orderField = null,$orderType = "ASC"){

        $obj =  Db::name($this->table)->where($where);

        if(!empty($page) && !empty($limit)){
            $obj = $obj->page($page, $limit);
        }

        if(!empty($orderField) && !empty($orderType)){
            $obj = $obj->order($orderField, $orderType);
        }

        $list = $obj->select();

        if($list){
           return $list->toArray();
        }
        return [];
    }



    /*
     * 某个用户是否报名某活动
     * */
    public function isHaveSignUp($active_id,$uid){
        $where=[];
        $where[] = ["uid","=",$uid];
        $where[] = ["active_id","=",$active_id];
        return (Db::name($this->table)->where($where)->count()) > 0 ;
    }



    /*
* 添加
* */
    public function addOne($data){
        return Db::name($this->table)->insertGetId($data);
    }

}